package in.muton.edushare.grading.panels.studentGrades;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.GradingItem;

public class StudentMainGradePanel extends EdusharePanel {
	private static final long serialVersionUID = -6113883458289923296L;
	public static final Log log = LogFactory
			.getLog(StudentMainGradePanel.class);

	StudentItemEdit studentItemEdit = null;
	
	public StudentMainGradePanel(String id, EduShareBasePage page,
			IModel<CourseInstances> cInst) {
		super(id, page);
		cInstModel = cInst;

		Form<Void> f = new Form<Void>("filterForm") {
			private static final long serialVersionUID = -1055290506635840780L;
			public void onSubmit() {
				if(getGradingItem(gradingItemModel.getObject()) == null){
					setActiveMode(VIEW_COURSE_GRADES);
				}else{
					setActiveMode(VIEW_ITEM_GRADES);
					studentItemEdit.setInitialized(false);
					setiGradeModel(getGradingItem(gradingItemModel.getObject()));
				}
			}
		};
		
		add(f);
		f.add(new DropDownChoice<String>("filterOption", this.gradingItemModel , 
				new ComponentListModel(cInst), new ComponentRenderer(cInstModel)));
		
		f = new Form<Void>("studentForm");
		f.add(new TextField<String>("studentName", new PropertyModel<String>(this,"studentName")));
		add(f);
		add(new StudentItemView("studentItemView", page, this));
		add(studentItemEdit = new StudentItemEdit("studentItemEdit", page, this));
		add(new StudentCourseView("studentCourseView", page, cInst, this));
		add(new StudentCourseEdit("studentCourseEdit", page, cInst, this));
	}

	public void setiGradeModel(IModel<GradingItem> iGradeModel) {
		this.iGradeModel = iGradeModel;
	}

	public IModel<GradingItem> getiGradeModel() {
		return iGradeModel;
	}

	public void setActiveMode(int activeMode) {
		this.activeMode = activeMode;
	}

	public int getActiveMode() {
		return activeMode;
	}

	public static final int VIEW_COURSE_GRADES = 10;
	public static final int VIEW_ITEM_GRADES = 20;
	public static final int EDIT_COURSE_GRADES = 30;
	public static final int EDIT_ITEM_GRADES = 40;

	private IModel<CourseInstances> cInstModel = null;

	public IModel<CourseInstances> getcInstModel() {
		return cInstModel;
	}

	public void setcInstModel(IModel<CourseInstances> cInstModel) {
		this.cInstModel = cInstModel;
	}
	
	
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

	public String getStudentName() {
		return studentName;
	}
	
	private IModel<GradingItem> getGradingItem(String itemName){
		List<GradingItem> itemList = cInstModel.getObject().getGradingItems();
		for(GradingItem g:itemList){
			if(g.getGradingItemName().equals(itemName))
				return new CayenneModel<GradingItem>(g);
		}
		return null;
	}
	
	private class ComponentRenderer implements IChoiceRenderer<String>{
		private static final long serialVersionUID = 4304652468739996326L;
		
		public ComponentRenderer(IModel<CourseInstances> cInst){
			this.cInst = cInst;
		}
		
		public Object getDisplayValue(String arg0) {
			return arg0;
		}

		public String getIdValue(String arg0, int arg1) {
			List<GradingItem> itemList = cInst.getObject().getGradingItems();
			for(GradingItem g:itemList){
				if(arg0.equals(g.getGradingItemName()))
					return g.getGradingItemId()+"";
				if(arg0.equals(ENTIRE_COURSE_STRING))
					return "0";
			}
			return "-1";
		}
		private IModel<CourseInstances> cInst = null;
	}
	
	private class ComponentListModel implements IModel<List<String>>{
		private static final long serialVersionUID = 7250887260953818586L;
		public ComponentListModel(IModel<CourseInstances> cInst){
			this.cInst = cInst;
		}
		public List<String> getObject() {
			List<String> compList = new ArrayList<String>();
			List<GradingItem> itemList = cInst.getObject().getGradingItems();
			for(GradingItem g:itemList){
				compList.add(g.getGradingItemName());
			}
			compList.add(ENTIRE_COURSE_STRING);
			return compList;
		}
		public void setObject(List<String> arg0) {
		}
		public void detach() {
		}
		private IModel<CourseInstances> cInst = null;
	}

	private String studentName = "";
	private Model<String> gradingItemModel = new Model<String>("");
	private IModel<GradingItem> iGradeModel = null;
	private int activeMode = VIEW_COURSE_GRADES;
	private String ENTIRE_COURSE_STRING = "Entire Course";
	
}
